<?php

class db_mysql
{
	private $host;
	private $database;
	private $user;
	private $password;
	
	private $charset	= "utf8";
	private $debug		= false;
	private $bSqlDebug	= false;
	public $sqlDebug	= "";
	private $sqlStr		= "";

	private $pconnect	= false;
	public $record		= array();

	private $linkid		= null;
	private $queryid	= null;

	private $isPing		= false;

	public function __construct($host,$user,$pass,$database,$isPing=false)
	{

		$this->host		= $host;
		$this->user		= $user;
		$this->password	= $pass;
		$this->database	= $database;
		$this->isPing	= $isPing;
	}

	public function getlinkid()
	{
		return $this->linkid;
	}

	public function connect()
	{
		if ( !is_resource($this->linkid)) {
			if ( $this->pconnect ) {
				$this->linkid = @mysql_pconnect($this->host, $this->user, $this->password);
			} else {
				$this->linkid = @mysql_connect($this->host, $this->user, $this->password);
			}
			if ( $this->linkid == 0 ) {
				return false;
			}
			@mysql_query("SET NAMES '".$this->charset."'", $this->linkid);
			if ( !@mysql_select_db($this->database, $this->linkid) ) {
				return false;
			}
		}
		return $this->linkid;
	}
	

	function start_trans(){
		@mysql_query("start transaction;",$this->linkid);
	}

	function commit(){
		@mysql_query("commit;",$this->linkid);
	}

	function rollback(){
		@mysql_query("rollback;",$this->linkid);
	}

	function query($sql)
	{
		if ($sql == '') return false;

		if (!$this->connect()) return false;

		if ( $this->queryid ) {
			$this->free();
		}

		if($this->bSqlDebug) {
			$this->sqlDebug .= $sql . "\n";
		}
		
		$this->sqlStr = $sql;
		$this->queryid = @mysql_query($this->sqlStr, $this->linkid);
		if ( !$this->queryid ) {
			return false;
		} else {
			return $this->queryid;
		}
	}

	function nextrecord()
	{
		if ( !$this->queryid ) {
			return false;
		}
		$this->record = @mysql_fetch_assoc($this->queryid);
		if($this->record)
			return true;
		return false;
	}
/**
 *
 * @return array
 */
	public function getAll($sql)
	{
		$result = array();
		
		$this->query($sql);
		
		while($this->nextrecord())
		{
			$result[] = $this->record;
		}
		return $result;
	}
/**
 *
 * @return bool
 */
	function insertid()
	{
		if( $result = mysql_insert_id($this->linkid) ) {
			return $result;
		} else {
			return false;
		}
	}
/**
 *
 * @return int
 */
	function affectedrows()
	{
		return @mysql_affected_rows($this->linkid);
	}
	
	public function free()
	{
		@mysql_free_result($this->queryid);
		$this->queryid	= 0;

		$this->sqlStr	= "";
		$this->sqlDebug = "";
		$this->record	= array();
	}

	public function close()
	{
		if (!empty($this->linkid) &&  $this->linkid != 0)
		{
			@mysql_close($this->linkid);
			$this->linkid = 0;
		}
	}
/**
 * error
 *
 * @return string
 */
    public function error() {
        $error = mysql_errno() . ":" . mysql_error();
        if($this->sqlStr != ''){
            $error .= "\n [ SQL语句 ] : ".$this->sqlStr;
        }
        return $error;
    }

	function __destruct(){
		$this->close();
	}
}
?>